import {
    CHANGE_LOADING,
    MENU_LIST_DATA,
    CHANGE_USERMSG,
} from './mutation-types.js';
import { getUaaListApi , getUaamsgApi} from "@/api/getData";
export default {
    async changeLoading({commit}, params) {
       await commit(CHANGE_LOADING, params)
    },
    async initMenuList({commit}){
        try {
            commit(CHANGE_LOADING, true);
            let data = await getUaaListApi();
            commit(CHANGE_LOADING, false);
            if(data && data.length > 0){
                commit(MENU_LIST_DATA,data);
                return Promise.resolve(true);
            }else{
                return Promise.resolve(false);
            }
        } catch (error) {
            commit(CHANGE_LOADING, false);
            console.log(error)
        }
    },
    // 控制页面访问权限
    async watchPageAuth({commit,state},params){
        if(params == "menu" || params == "login"){
            return Promise.resolve(true)
        }
        var pass = false;
        if(state.menuListData.length > 0){
            state.menuListData.map((item)=>{
                if(item.url == params){
                    pass = true;
                }
            });
            return Promise.resolve(pass);
        }else{
            let data = await getUaaListApi();
            if(data && data.length > 0){
                commit(MENU_LIST_DATA,data);
                data.map((item)=>{
                    if(item.url == params){
                        pass = true;
                    }
                });
                return Promise.resolve(pass);
            }else{
                return Promise.resolve(pass);
            }
        }    
    },
    // 获取页面的用户信息
    async getThismsgUaa({commit,state},params){
        try {
            console.log(params)
            if(state.userMsg.id && !params){
                return Promise.resolve(state.userMsg);
            }else{
                let data = await getUaamsgApi();
                if(data){
                    commit(CHANGE_USERMSG,data);
                    return Promise.resolve(data);
                }else{
                    return Promise.resolve({})
                }
            }
        } catch (error) {
            console.log(error)
        }
    }
}